new Vue({
	el: '#app',
	data: {
		uid: '',
		api_token: '',
		time: '',
		tid: '',
		sid: '',
		type: '',
		study_list: [],
		lesson: [],
		teacher_list: [],
		study_course_list: []
	},
	mounted: function() {
		console.log("mounted end");
		this.uid = $api.getStorage('uid');
		this.api_token = $api.getStorage('api_token');
		this.init();
	},
	methods: {
		init: function() {
			var self = this;
			apiready = function() {
				var fid = api.pageParam;
				console.log(fid.time);
				self.time = fid.time;
				//loading加载
				self.isS();
			}
		},
		isS: function() {
			var self = this;
			var UILoading = api.require('UILoading');
			if(self.isShow) {
				console.log(self.loadingId);
				UILoading.closeFlower({
					id: self.loadingId
				});
			} else {
				UILoading.flower({
					center: {
						x: api.winWidth / 2.0,
						y: api.winHeight / 2.0
					},
					size: 30,
					fixed: true
				}, function(ret) {
					console.log(JSON.stringify(ret));
					self.loadingId = ret.id;
				});
				self.getOrderClass();
			}
		},
		//获取该日期的排课时间和预约情况
		getOrderClass: function() {
			var self = this;
			var data = {
				uid: self.uid,
				token: self.api_token,
				day: self.time,
				study_id: self.sid,
				teacher_id: self.tid,
				type: self.type
			};
			$.post(base + config.study_course_list, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.study_list = msg.study_list;
					self.teacher_list = msg.teacher_list;
					self.study_course_list = msg.study_course_list;
					self.isShow = true;
					self.isS();
				} else {
					self.isShow = true;
					self.isS();
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//取消预约
		cacel: function(id) {
			var self = this;
			console.log(id);
			var data = {
				uid: self.uid,
				token: self.api_token,
				id: id
			};
			$.post(base + config.study_lesson_cancel, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					api.toast({
						msg: "取消成功",
						duration: 2000,
						location: 'middle'
					});
					self.getOrderClass();
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//关闭本页面
		close: function() {
			api.openWin({
				name: 'calendar',
				url: 'calendar.html',
				reload: true,
				pageParam:{
					name: 'curriculumSchedule'
				}
			});
			api.closeWin();
		},
		//选择课程
		setSid: function(event) {
			this.sid = parseInt(event.target.value)
			console.log(this.sid);
			this.isShow = false;
			this.isS();
		},
		//选择老师
		setTid: function(tid) {
			this.tid = parseInt(event.target.value)
			console.log(this.tid);
			this.isShow = false;
			this.isS();
		},
		//选择时间
		setType: function(type) {
			this.type = type
			console.log(this.type);
			this.isShow = false;
			this.isS();
		},
		//预约
		orderClass: function(hour, id) {
			var self = this;
			console.log(id);
			var data = {
				uid: self.uid,
				token: self.api_token,
				day: self.time,
				study_course_id: id,
				hour: hour
			};
			$.post(base + config.study_lesson_apply, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					api.toast({
						msg: "预约成功",
						duration: 2000,
						location: 'middle'
					});
					self.getOrderClass();
				} else {
					self.isShow = true;
					self.isS();
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		}
	}
})